<template>
  <div class="app-container">
    <div class="main_concent">
      <el-tabs type="border-card">
        <el-tab-pane :label="$t('writeOff.picku')" class="tabs_item">
          <el-form :model="data.form" :label-width="language == 'en' ? '110' : '80'" class="mt-4" style="max-width: 900px">
            <span :class="language == 'en' ? 'check relative top-8 ml-8' : 'check relative top-8 ml-3'">*</span>
            <el-form-item :label="$t('writeOff.machinery')">
              <el-select v-model="data.form.m_id" :placeholder="$t('message.pleaseSelect')" @change="changeMachine">
                <el-option v-for="(item, index) in data.getmachineList" :key="item.m_id"
                  :label="item.m_id + '-' + item.machine_name" :value="item.m_id" />
              </el-select>
            </el-form-item>
            <span :class="language == 'en' ? 'check relative top-8 ml-0' : 'check relative top-8'">*</span>
            <el-form-item :label="$t('writeOff.code')" :class="!data.isPickupCode ? 'pickupCode' : ''">
              <el-input v-model="data.form.code" :disabled="data.isPickupCode" />
              <el-icon class="cameraFilled" size="20" @click="openQrcode" v-if="!data.isPickupCode">
                <img src="@/assets/sao.png" alt="">
              </el-icon>
            </el-form-item>
          </el-form>
          <div :class="isFixed ? 'isFixed buts' : 'burt'">
            <div :class="isFixed ? 'butFixed' : 'but'" ref="btn">
              <div class="button">
                <el-button v-prmis="'activity_pick_queryByCodeMachine'" type="primary" @click="onQuery">{{
                  $t('writeOff.inquiry') }}</el-button>
                <el-button v-prmis="'activity_pick_usePickCode'" type="primary" @click="onWriteOff" :disabled="data.isdisabled || data.machineList.data && data.machineList.data.pick_type == 3
                  || data.machineList.state !== 200">{{ $t('writeOff.Verific') }}</el-button>
              </div>
            </div>
          </div>
          <!-- 取货码查询 -->
          <div class="concent_one" v-if="isQuery">
            <!-- 查询成功 -->
            <div class="one_msg" v-if="data.machineList.state == 200">
              <div class="msg_left">
                <div class="error_icon">
                  <el-icon color="rgb(50 211 50)" class="icon">
                    <SuccessFilled />
                  </el-icon>
                  <p class="msg_title error_itle">{{ $t('writeOff.valid') }}</p>
                </div>
                <p>{{ $t('goods.corner.activitName') }}{{ data.machineList.data && data.machineList.data.pick_name ?
                  data.machineList.data.pick_name : '--' }}</p>
                <p>{{ $t('marketing.reduction.desc') }}：{{ data.machineList.data &&
                  data.machineList.data.desc ? data.machineList.data.desc : '--' }}</p>
                <p>{{ $t('date.activityTime') }}：{{ getPlevel.gettime(data.machineList.data &&
                  data.machineList.data.start_time, 1) }} ~ {{ getPlevel.gettime(data.machineList.data
                    && data.machineList.data.end_time, 1) }}</p>
                <p>{{ $t('writeOff.type') }}: {{ setPickType(data.machineList.data && data.machineList.data.pick_type)
                  }}
                </p>
              </div>
              <div class="shops_img" v-if="data.machineList.data && data.machineList.data.activity_goods">
                <div :class="item.className ? 'img_item isHighlight' : 'img_item'"
                  v-for="(item, index) in data.machineList.data.activity_goods" @click="onItem(item)">
                  <div :class="item.className ? 'radio isRadio' : 'radio'" @click="onItem(item)"></div>
                  <img v-if="item.pic" class="img" :src="item.pic" alt="">
                  <img v-else class="img" src="@/assets/shop.png" alt="">
                  <div class="item_right">
                    <p class="name" :title="item.g_name">{{ item.g_name }}</p>
                    <p>{{ item.sku ? item.sku : '--' }}</p>
                  </div>
                </div>
              </div>
            </div>
            <!-- 查询异常 -->
            <div class="one_msg" v-else>
              <div class="error_icon">
                <el-icon class="icon" color="red">
                  <WarningFilled />
                </el-icon>
                <p class="msg_title error_itle">{{ data.machineList.msg }}</p>
              </div>
            </div>
          </div>
          <!-- 取货码核销 -->
          <div class="concent_tow" v-if="isWriteOff">
            <!-- 核销成功 -->
            <div class="main_success" v-if="data.pickCodeList.state == 200">
              <div class="shop_msg">
                <div class="concent_center">
                  <div class="error_icon">
                    <el-icon color="rgb(50 211 50)" class="icon">
                      <SuccessFilled />
                    </el-icon>
                    <p class="msg_title error_itle">{{ data.pickCodeList.msg }}</p>
                  </div>
                </div>
              </div>
            </div>
            <!-- 核销异常 -->
            <div class="main_error" v-else ref="errorRef">
              <div class="concent_center">
                <div class="error_icon">
                  <el-icon class="icon" color="red">
                    <WarningFilled />
                  </el-icon>
                  <p class="msg_title error_itle">{{ data.pickCodeList.msg }}</p>
                </div>
                <p class="pickup_p" v-if="data.pickCodeList.state == 1112">
                  {{ $t('writeOff.your') }}【{{ data.pickCodeList.data ? data.pickCodeList.data.code : '--' }}】{{
                    $t('writeOff.used') }}【{{ getPlevel.gettime(data.pickCodeList.data.used_time, 3) }}】{{
                    $t('writeOff.reused') }}
                </p>
                <p class="pickup_p" v-if="data.pickCodeList.state == 1113">
                  {{ $t('writeOff.your') }}【{{ data.pickCodeList.data ? data.pickCodeList.data.code : '--' }}】{{
                    $t('writeOff.expired') }}：【{{ getPlevel.gettime(data.pickCodeList.data.used_time, 3) }}】，{{
                    $t('writeOff.verification') }}
                </p>
                <p class="pickup_p" v-if="data.pickCodeList.state == 1104">
                  {{ $t('writeOff.your') }}【{{ data.pickCodeList.data ? data.pickCodeList.data.code : '--' }}】{{
                    $t('writeOff.use') }}
                </p>
                <p class="pickup_p" v-if="data.pickCodeList.state == 1103">
                  {{ $t('writeOff.your') }}【{{ data.pickCodeList.data ? data.pickCodeList.data.code : '--' }}】{{
                    $t('writeOff.ended') }}
                </p>
                <p class="pickup_p" v-if="data.pickCodeList.state == 1115">
                  {{ $t('writeOff.your') }}【{{ data.pickCodeList.data ? data.pickCodeList.data.code : '--' }}】{{
                    $t('writeOff.useing') }}
                </p>
                <p class="pickup_p" v-if="data.pickCodeList.state == 1114">
                  {{ $t('writeOff.your') }}【{{ data.pickCodeList.data ? data.pickCodeList.data.code : '--' }}】{{
                    $t('writeOff.invalidated') }}
                </p>
              </div>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
    <QrCode v-if="isQrCode" ref="refQrCode" @clickBack="clickBack" @qrCodeSuccess="qrCodeSuccess" />
  </div>
</template>

<script lang="ts" setup>
import { useI18n } from 'vue-i18n'
const { t: $t } = useI18n()
import { getmachineList, getCodeMachine, getUsePickCode } from '@/api/writeOff';
import getPlevel from '@/utils/level'
import { onUnmounted, onMounted, ref } from 'vue'
import QrCode from './scanCode/index.vue'
let language = ref()
const { locale } = useI18n()
onMounted(() => {
  language.value = locale.value
})
let isWriteOff = ref<boolean>(false)
let isQuery = ref<boolean>(false)
let isFixed = ref<boolean>(false)
let isQrCode = ref(false)
const btn = ref()
let refQrCode = ref()
let qrCodeSuccess = (res) => {
  data.form.code = res
  isQrCode.value = false
}
let openQrcode = () => {
  isQrCode.value = true
}
let clickBack = () => {
  isQrCode.value = false
}
let handleScroll = () => {
  var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
  var Top = btn.value.getBoundingClientRect().top
  if (Top < 50) {
    isFixed.value = true
  } else if (scrollTop == 0) {
    isFixed.value = false
  }
}
let setPickType = (type: number) => {
  if (type == 2) {
    return '用户自选'
  }
  if (type == 3) {
    return '预售订单(该类型不在手机端核销)'
  }
  return '--'
}
let onItem = (row: any) => {
  data.form.g_id = row.g_id
  data.machineList.data.activity_goods.forEach(item => {
    item.className = ''
  });
  row.className = 'isHighlight'
}
let data = reactive({
  form: {
    machine: '',
    m_id: '',
    code: '',
    g_id: ''
  },
  isPickupCode: true,
  getmachineList: [],
  machineList: {},
  pickCodeList: {},
  isdisabled: true
})
let changeMachine = (val: string) => {
  data.isPickupCode = false
}
let getmachineData = () => {
  getmachineList({}).then(res => {
    if (res.state == 200) {
      data.getmachineList = res.data
    }
  })
}
getmachineData()
let onQuery = () => {
  if (data.form.m_id && data.form.code) {
    data.isdisabled = false
  }
  let pram = {
    m_id: data.form.m_id + '', // 3
    code: data.form.code // 67557019
  }
  if (!data.form.m_id) {
    ElMessage({
      message: $t('message.machineUnempty'),
      type: 'error'
    })
    return
  }
  if (!data.form.code) {
    ElMessage({
      message: $t('message.pickupCode'),
      type: 'error'
    })
    return
  }
  isWriteOff.value = false
  getCodeMachine(pram).then(res => {
    if (res.state == 200) {
      isQuery.value = true
      data.machineList = res
    }
  }).catch(err => {
    data.machineList = {}
    data.machineList.state = err.state
    data.machineList.msg = err.msg
    isQuery.value = true
  })
}

const errorLoald = ref(null);
let onWriteOff = async () => {
  data.form.g_id = '64'
  if (!data.form.g_id) {
    ElMessage({
      message: $t('message.pleaseisEmpty'),
      type: 'error'
    })
    return
  }
  let pram = {
    m_id: data.form.m_id + '',
    pick_code: data.form.code,
    g_id: data.form.g_id + ''
  }
  if (!data.form.m_id) {
    ElMessage({
      message: $t('message.machineUnempty'),
      type: 'error'
    })
    return
  }
  if (!data.form.code) {
    ElMessage({
      message: $t('message.pickupCode'),
      type: 'error'
    })
    return
  }
  getUsePickCode(pram).then(res => {
    if (res.state == 200) {
      data.pickCodeList = res
    }
  }).catch(err => {
    data.pickCodeList = {}
    data.pickCodeList.state = err.state
    data.pickCodeList.data = err.data
    data.pickCodeList.msg = err.msg
    errorLoald
  })
  isWriteOff.value = true
}
onMounted(() => {
  window.addEventListener('scroll', handleScroll);
})
onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll)
})
</script>

<style scoped lang="scss">
.concent_tow {
  clear: both;
}

.check {
  color: red;
}

.but {
  margin-bottom: 20px;
  float: right;
}

/* 当屏幕宽度小于或等于768像素时，应用以下样式 */
@media (max-width:780px) {
  .concent_one {
    background-size: 5px 100%;
    background-position: 0 0, 0 100%;
    border-bottom: 1px solid #eee;
    border-top: 1px solid #eee;
    border-radius: 5px;
    margin-bottom: 15px;
  }

  .cameraFilled {
    position: absolute;
    right: 0px;
  }

  .pickupCode {
    .el-input.is-disabled {
      width: 90%;
    }

    .el-input {
      width: 90%;
    }
  }

  .button {
    .el-button {
      width: 86px;
    }
  }

  .isFixed {
    position: fixed;
    top: 49px;
    right: 0;
    left: 0;
    z-index: 1000;
    background: #ffffff;
    padding: 10px 20px;
    float: right;
    border-top: 1px solid #eee;
  }

  .butFixed {
    float: right;
  }

  .one_msg {
    .isRadio {
      background: #409EFF;
    }

    .radio {
      width: 20px;
      height: 20px;
      border-radius: 50%;
      border: 1px solid #6d6b6b;
      align-self: center;
      margin-right: 15px;
    }

    .shops_img {
      margin-top: 20px;
    }

    .img_item:last-child {
      border-bottom: none;
    }

    .img_item {
      display: flex;
      border-bottom: 1px solid #eee;
      padding: 16px 0px;

      .item_right {
        margin-left: 10px;
        width: 50%;
      }
    }

    padding:10px;

    .img {
      width: 90px;
      height: 90px;
    }
  }

  .msg_left {
    p {
      line-height: 35px;
    }
  }

  .msg_title {
    font-weight: bold;
    font-size: 17px;
  }

  .icon {
    font-size: 30px;
  }

  .main_error {
    padding: 10px 10px 40px 10px;
  }

  .error_icon {
    display: flex;
  }

  .error_itle {
    margin-left: 10px;
  }

  // .concent_center{
  //   width: 65%;
  // }
  .main_success {
    padding: 10px;

    .shop_name {
      height: 25px;
      overflow: hidden;
      white-space: nowrap;
      /* 确保文本在一行内显示 */
      text-overflow: ellipsis;
      /* 使用省略号表示文本溢出 */
    }

    .shop_msg {
      display: flex;
    }

    .shop_img {
      margin-left: 15px;
      align-self: center;

      img {
        width: 90px;
        height: 90px;
      }
    }
  }
}

@media (min-width: 781px) {
  .concent_one {
    background-size: 5px 100%;
    background-position: 0 0, 0 100%;
    border: 1px solid #eee;
    border-radius: 5px;
    margin-bottom: 15px;
  }

  .cameraFilled {
    position: absolute;
    right: -30px;
    display: none;
  }

  .isFixed {
    position: fixed;
    top: 49px;
    right: 0;
    left: 0;
    z-index: 1000;
    background: #ffffff;
    padding: 10px 20px;
    border-top: 1px solid #eee;
  }

  .butFixed {
    .button {
      float: right;
    }

    padding: 0px 20px;
    position: relative;
    // width: 70%;
    margin: auto;
    background-color: red;
  }

  .one_msg {
    .item_right {
      width: 70%;
      margin: auto;
    }

    padding:30px 20px 40px 30px;

    .isHighlight {
      box-shadow: 1px 1px 5px 1px #409EFF;
      color: #409EFF;
    }

    .img_item:hover {
      box-shadow: 1px 1px 5px 1px #409EFF;
    }

    .img_item {
      display: flex;
      width: 360px;
      padding: 30px;
      margin-top: 15px;
      margin-left: 15px;
      border-radius: 5px;
      text-align: center;
      border: 1px solid #eee;
      display: inline-block;
      cursor: pointer;

      .img {
        width: 120px;
        height: 120px;
        margin: 20px auto;
      }
    }

    .msg_left {
      min-height: 30%;
      margin-right: 50px;
    }

    .name {
      height: 20px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }

    .msg_left {
      p {
        line-height: 40px;
      }
    }
  }

  .main_success {
    padding: 40px;
  }

  .icon {
    font-size: 40px;
  }

  .error_icon {
    display: flex;
  }

  .main_error {
    padding: 30px 30px 40px 30px;
  }

  .concent_center {
    min-width: 300px;
  }

  .main_concent {
    .shop_img {
      padding: 30px 0px 50px 0px;
      width: 100%;

      img {
        margin: auto;
        width: 160px;
        height: 160px;
      }
    }
  }

  .error_itle {
    margin-left: 20px;
  }

  .msg_title {
    font-weight: bold;
    font-size: 26px;
  }

  .shop_msg {
    display: flex;

    p {
      line-height: 40px;
    }
  }
}

.concent_one {
  clear: both;
}

.main_success {
  background-size: 5px 100%;
  background-position: 0 0, 0 100%;
  border: 1px solid #eee;
  border-radius: 5px;
}

.main_error {
  background-size: 5px 100%;
  background-position: 0 0, 0 100%;
  margin-bottom: 10px;
  border: 1px solid #eee;
  border-radius: 5px;
  margin-top: 15px;

  .pickup_p {
    padding-top: 10px;
  }
}

.main_concent {
  margin: auto;
  margin-top: 15px;
}

.app-main {
  width: 100%;
  position: relative;
  overflow: hidden;
  padding: 0px 20px;
  min-width: 300px;
}

.fixed-header+.app-main {
  padding-top: 50px;
}
</style>